//
//  YYPickerView.m
//  drivingSchool
//
//  Created by 子yu 刘 on 2018/8/18.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "YYPickerView.h"
@interface YYPickerView()
@property (nonatomic ,strong)UIPickerView * picker;
@property (nonatomic ,assign) NSInteger selectRow;

@end
@implementation YYPickerView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self initUI];
    }
    return self;
}

- (void)initUI{
    self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    UIView * bgView = [[UIView alloc] init];
    bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.height.mas_offset(240);
    }];
    UIView * btnView = [[UIView alloc] init];
    [bgView addSubview:btnView];
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_offset(48);
    }];
    
    UIButton * left = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnView addSubview:left];
    [left setTitle:@"取消" forState:UIControlStateNormal];
    left.titleLabel.font = [UIFont systemFontOfSize:16.0];
    [left setTitleColor:[UIColor colorWithHexString:@"666666"] forState:UIControlStateNormal];
    [left mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(16);
        make.centerY.mas_equalTo(0);
    }];
    [left addTarget:self action:@selector(cancel) forControlEvents:UIControlEventTouchUpInside];
    UIButton * right = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnView addSubview:right];
    [right setTitle:@"确定" forState:UIControlStateNormal];
    right.titleLabel.font = [UIFont systemFontOfSize:16.0];
    [right setTitleColor:[UIColor colorWithHexString:@"01c8fb"] forState:UIControlStateNormal];
    [right mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-16);
        make.centerY.mas_equalTo(0);
    }];
    [right addTarget:self action:@selector(corforme) forControlEvents:UIControlEventTouchUpInside];
    
    UIPickerView * picker = [[UIPickerView alloc] init];
    picker.delegate = self;
    picker.dataSource = self;
    [bgView addSubview:picker];
    [picker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(48);
        make.left.right.bottom.mas_equalTo(16);
    }];
    _picker = picker;
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _dataArr.count;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 42;
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    //设置分割线的颜色
    for(UIView *singleLine in pickerView.subviews)
    {
        if (singleLine.frame.size.height < 2)
        {
            singleLine.backgroundColor = [UIColor colorWithHexString:@"bdbdbd"];
        }
    }
    //设置文字的属性（改变picker中字体的颜色大小）
    UILabel *reasonLabel = [UILabel new];
    reasonLabel.textAlignment = NSTextAlignmentCenter;
    reasonLabel.text = _dataArr[row];;
    reasonLabel.font = [UIFont systemFontOfSize:14];
    reasonLabel.textColor = [UIColor colorWithHexString:@"666666"];
    //改变选中行颜色（设置一个全局变量，在选择结束后获取到当前显示行，记录下来，刷新picker）
    if (row == _selectRow) {
        //改变当前显示行的字体颜色，如果你愿意，也可以改变字体大小，状态
        reasonLabel.textColor = DEF_COLOR_MAIN;
    }
    
    return reasonLabel;
}

// 选中行显示在label上
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
   // ReasonModel *reasonModel = _reasonArray[row];
    //resaonType = [NSString stringWithFormat:@"%@-%@",reasonModel.sh_reason_id,reasonModel.sh_reason_text];
    //记录下滚动结束时的行数
    _selectRow = row;
    //刷新picker，看上面的代理
    [pickerView reloadComponent:component];
}
- (void)cancel{
    
    [self removeFromSuperview];
}

- (void)corforme{
    if (_block) {
        self.block(_selectRow);
    }
     [self removeFromSuperview];
}

-(void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
    [self.picker reloadAllComponents];
}


@end
